from twisted.internet import protocol, reactor, defer, ssl
from twisted.protocols import basic
from twisted.application import internet

from ConfigParser import SafeConfigParser

class alertProto(basic.NetstringReceiver):
    
    def sendMsg(self, msg):
        self.sendString(msg)

class alertSender(object):

    def __init__(self):
        self.name = "tcp"

    def configure(self, servers):
        self.servers = servers
        self.sslenabled = False
        self.conf = SafeConfigParser()
        self.conf.read('/home/zeno/diploma/src/PyHWeb/PyHMon-Web-Interface/' +\
                       'pyhweb.conf')
        if self.conf.has_option('global','mysslcert'):
            self.sslenabled = True
    
    def gotproto(self, p):
        print "FFF"
        p.sendMsg(self.msg)
        p.transport.loseConnection()

    def send(self, addr_and_port):
        c = protocol.ClientCreator(reactor, alertProto)
        if self.sslenabled:
            # read the configuration:
            myKey = self.conf.get('global', 'mysslcert')
            trustedCA = self.conf.get('global', 'casslcert')
            
            # read the certificate files:
            fd = open(myKey,'r')
            theCert = ssl.PrivateCertificate.loadPEM(fd.read())
            fd.close()
            fd = open(trustedCA,'r')
            theCA = ssl.Certificate.loadPEM(fd.read())
            fd.close()
            ctx = theCert.options(theCA)
            
            # set the SSL version to v3:
            ctx.method = ssl.SSL.SSLv3_METHOD
            
            # If True, verify certificates received from the peer and fail
            # the handshake if verification fails. Otherwise, allow anonymous
            # sessions and sessions with certificates which fail validation.
            ctx.verify = True

            # Depth in certificate chain down to which to verify.
            ctx.verifyDepth = 1
       
            # If True, do not allow anonymous sessions.
            ctx.requireCertification = True
       
            # If True, do not re-verify the certificate on session resumption.
            ctx.verifyOnce = True
       
            # If True, generate a new key whenever ephemeral DH parameters are
            # used to prevent small subgroup attacks.
            ctx.enableSingleUseKeys = True
       
            # If True, set a session ID on each context. This allows a shortened
            # handshake to be used when a known client reconnects.
            ctx.enableSessions = True
       
            # If True, enable various non-spec protocol fixes for broken
            # SSL implementations.
            ctx.fixBrokenPeers = False
            d = c.connectSSL(addr_and_port[0], addr_and_port[1], ctx)
        else:
            d = c.connectTCP(addr_and_port[0], addr_and_port[1])
        d.addCallback(self.gotproto)
        

    def sendAlert(self, msg):
        self.msg = msg
        for i in self.servers.keys():
            self.send(self.servers[i])
        reactor.run()